package com.hitqz.robot.dispatch.netty.strategy.job;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.cache.Cache;
import com.hitqz.robot.api.business.dto.RobotClientSummaryDto;
import com.hitqz.robot.api.business.entity.JobCachesEntity;
import com.hitqz.robot.api.business.entity.RobotEntity;
import com.hitqz.robot.api.business.entity.RobotJobEntity;
import com.hitqz.robot.api.business.enums.WorkStatusEnum;
import com.hitqz.robot.biz.service.JobCachesService;
import com.hitqz.robot.dispatch.netty.model.DispatchJobDto;
import com.hitqz.robot.dispatch.netty.strategy.JobStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 狭小空间下AGV进行任务派送  昆明的压装间的任务
 * @author xupkun
 * @date 2025/4/17
 */
@Service("singleCarStrategy")
@RequiredArgsConstructor
@Slf4j
public class SingleCarStrategy  implements JobStrategy  {

    private final JobCachesService jobCachesService;

    @Override
    public DispatchJobDto dispatchJob(Integer groupId, String taskContent) {
        Cache<String, RobotClientSummaryDto> posInfoDtoCache = SpringUtil.getBean("robotCache");
        Cache<Integer, List<RobotEntity>> robotDispatch = SpringUtil.getBean("robotDispatch");
        // 查询当前组下的所有机器人
        List<RobotEntity> currentGroupRobot = robotDispatch.getIfPresent(groupId);
        // 检查列表是否为空
        if (currentGroupRobot == null) {
            return null;
        }
        DispatchJobDto dto = null;
        List<JobCachesEntity> jobCachesEntityList = jobCachesService.list(Wrappers.<JobCachesEntity>lambdaQuery()
                .eq(JobCachesEntity::getGroupId, groupId)
                .orderByAsc(JobCachesEntity::getSn));
        if (CollUtil.isEmpty(jobCachesEntityList)) {
            return null;
        }
        jobCachesEntityList.sort((job1, job2) -> {
            boolean isJob1Forklift = job1.getTaskName().contains("叉车");
            boolean isJob2Forklift = job2.getTaskName().contains("叉车");
            if (isJob1Forklift && !isJob2Forklift) {
                return -1;
            } else if (!isJob1Forklift && isJob2Forklift) {
                return 1;
            } else {
                return job1.getSn().compareTo(job2.getSn());
            }
        });

        for (String clientId : posInfoDtoCache.asMap().keySet()) {
            RobotClientSummaryDto robotClientSummaryDto = posInfoDtoCache.getIfPresent(clientId);
            assert robotClientSummaryDto != null;
            if (robotClientSummaryDto.getStatus().getWorkStatus() != Integer.parseInt(WorkStatusEnum.FREE.getCode())) {
                log.info("其中有一台在进行任务，暂时不分配任务");
                break;
            }
            // 判断是不是都在等待点
            String nearStation = robotClientSummaryDto.getPosition().getLaserPos().getNearPosCode();
            log.info("最近的点是:" + nearStation);

        }

        return dto;
    }
}
